use std::io;
fn main() {
    //get input
    println!("Please enter Celsius(number end with C) or Fahrenheit(number end with F): ");
    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read line!");

    //set flag for end of input
    let mut ctf = false;
    let mut ftc = false;
    let flag = input.trim().ends_with("C");
    if flag {
        ctf = true;
    }
    let flag = input.trim().ends_with("F");
    if flag {
        ftc = true;
    }

    //parse num from input
    let num = input.trim().trim_end_matches("C").trim_end_matches("F");
    let num: f64 = num.parse().expect("Please enter a number!");

    //calculate result
    if ctf {
        let result = c_to_f(num);
        println!("{num} Celsius is {result:.2} Fahrenheit.");
    } else if ftc {
        let result = f_to_c(num);
        println!("{num} Fahrenheit is {result:.2} Celsius.");
    } else {
        println!("Please end with C or F");
    }
}

fn c_to_f(num: f64) -> f64 {
    (num * 9.0 / 5.0) + 32.0
}
fn f_to_c(num: f64) -> f64 {
    (num - 32.0) * 5.0 / 9.0
}
